package com.rd.emf.geneditor.view.dialogs;

import java.util.Map;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

@SuppressWarnings("unchecked")
public class InstanceCreatorDialog extends TitleAreaDialog {

	private ComboViewer comboViewer;
	private Combo combo;
	private Map<EPackage, EStructuralFeature> availableFeatures = null;
	private Map.Entry<EPackage, EStructuralFeature> selectedItem = null;

	public InstanceCreatorDialog(Shell parentShell, Map<EPackage, EStructuralFeature> availableFeatures) {
		super(parentShell);
		this.availableFeatures = availableFeatures;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		setTitle("Instance to create");

		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		container.setLayout(gridLayout);

		comboViewer = new ComboViewer(container, SWT.BORDER);
		combo = comboViewer.getCombo();
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		comboViewer.setContentProvider(new ArrayContentProvider());
		comboViewer.setLabelProvider(new ILabelProvider() {

			@Override
			public void removeListener(ILabelProviderListener listener) {

			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			@Override
			public void dispose() {

			}

			@Override
			public void addListener(ILabelProviderListener listener) {

			}

			@Override
			public String getText(Object element) {
				String text = null;
				if (element instanceof Map.Entry<?, ?>) {
					Map.Entry<EPackage, EStructuralFeature> entry = (Map.Entry<EPackage, EStructuralFeature>) element;
					text = "[" + entry.getKey().getNsPrefix() + "] " + entry.getValue().getName();
				} else {
					text = element.toString();
				}
				return text;
			}

			@Override
			public Image getImage(Object element) {
				return null;
			}
		});

		comboViewer.setInput(availableFeatures.entrySet());

		return container;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(400, 250);
	}

	@Override
	protected void okPressed() {
		if (comboViewer.getSelection() instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) comboViewer.getSelection();
			if (structuredSelection.getFirstElement() instanceof Map.Entry<?, ?>) {
				selectedItem = (Map.Entry<EPackage, EStructuralFeature>) structuredSelection.getFirstElement();
			}
		}
		super.okPressed();
	}

	public Map.Entry<EPackage, EStructuralFeature> getSelectedItem() {
		return this.selectedItem;
	}

}